package ref;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import ref.IAnalysisHandle;
import ref.OracleConn;

public abstract class AAnalysisHandle implements IAnalysisHandle {
	
	public int getFileNum(String path) throws Exception {
		int count=0;
		File file=new File(path);
		File[] files= file.listFiles();
		for(int i=0;i<files.length;i++){
			if(files[i].getName().indexOf(".htm")!=-1){
				count++;
			}
		}
		OracleConn.log("In the end ,there are " + count + " htm files. ");
		return count++;
	}

	public String getHtmlDoc(String path) throws Exception {
		StringBuffer document = new StringBuffer();
		InputStreamReader fr = null;
		BufferedReader br = null;
		String line = null;
		try {
//			fr = new InputStreamReader(new FileInputStream(path), "utf-8");
//			fr = new InputStreamReader(new FileInputStream(path), "gb2312");
			fr = new InputStreamReader(new FileInputStream(path), "gbk");
			br = new BufferedReader(fr);
			while ((line = br.readLine()) != null) {
				document.append(line);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (br != null) {
					br.close();
				}
				if (fr != null) {
					fr.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return document.toString();
	}

	public boolean checkTargetTable(Connection oracleConn) {
		Statement stat = null;
		ResultSet rs = null;
		try {
			stat = oracleConn.createStatement();
			rs = stat.executeQuery("select count(0) from tabs where lower(table_name)='"
							+ getTargetTable() + "'");
			int c = 0;
			if (rs.next()) {
				c = rs.getInt(1);
			}

			if (c == 0) {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace(System.err);
			return false;
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (Exception e) {
					e.printStackTrace(System.err);
				}
				rs = null;
			}
			if (stat != null) {
				try {
					stat.close();
				} catch (Exception e) {
					e.printStackTrace(System.err);
				}
				stat = null;
			}
		}

		return true;
	}
}